package com.cbs.bluetooth.device.qunsuo.test;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import app.akexorcist.bluetotohspp.library.BluetoothSPP;
import com.cbs.bluetooth.device.qunsuo.PrintFailureState;
import com.cbs.bluetooth.device.qunsuo.PrintListener;
import com.cbs.bluetooth.device.qunsuo.PrintState;
import com.cbs.bluetooth.device.qunsuo.Printer;
import com.cbs.log.L;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by broche on 10/9/15.
 */
public class MainActivity extends Activity {
    private static final String TAG = MainActivity.class.getName();
    private Printer printer = null;
    private BluetoothSPP bluetoothSPP;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_cbs);
        L.LOG_LEVEL = L.DEBUG;

        printer = new Printer(this, new PrintListener() {
            public void onSuccess() {
                L.e(TAG, "onsuccess");
            }

            public void onStateChanged(PrintState state) {
                L.e(TAG, "state changed: " + state.toString());
            }

            public void onFailure(PrintFailureState state) {
                L.e(TAG, "onfailure: " + state.toString());
            }
        });
        printer.setAndroid(true);

        final Button printView = (Button) findViewById(R.id.print);
        printView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                List<String> list = new ArrayList<String>();
                list.add("test");
                list.add("test2");
                list.add("test3");
                list.add("test4");
                printer.print(list, "C0:45:33:00:11:3F");
//                printer.print(list, "68:DF:DD:F5:7D:CF");
            }
        });
        Button cancelView = (Button) findViewById(R.id.cancel);
        cancelView.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                printer.cancel();
            }
        });

        Button serviceButton = (Button) findViewById(R.id.service);
        serviceButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bluetoothSPP = new BluetoothSPP(MainActivity.this);
                bluetoothSPP.setOnDataReceivedListener(new BluetoothSPP.OnDataReceivedListener() {
                    @Override
                    public void onDataReceived(byte[] data, String message) {
                        String str;
                        try {
                            str = new String(data, "gbk");
                        } catch (UnsupportedEncodingException e) {
                            str = new String(data);
                        }
                        Toast.makeText(MainActivity.this, str, Toast.LENGTH_SHORT).show();
                        Log.e(TAG, String.format("data: %s\tmessage: %s", str, message));
                    }
                });
                bluetoothSPP.setBluetoothStateListener(new BluetoothSPP.BluetoothStateListener() {
                    @Override
                    public void onServiceStateChanged(int state) {
                        Log.e(TAG, "state: " + state);
                    }
                });
                bluetoothSPP.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
                    @Override
                    public void onDeviceConnected(String name, String address) {
                        Log.e(TAG, String.format("connected: name: %s\t address: %s", name, address));
                    }

                    @Override
                    public void onDeviceDisconnected() {
                        Log.e(TAG, "disconnected");
                    }

                    @Override
                    public void onDeviceConnectionFailed() {
                        Log.e(TAG, "connect failed");
                    }
                });
                bluetoothSPP.setupService();
                bluetoothSPP.startService(true);

            }
        });

        Button connectButton = (Button) findViewById(R.id.connect);
        connectButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bluetoothSPP.connect("68:DF:DD:F5:7D:CF");
            }
        });

        Button sendButton = (Button) findViewById(R.id.send);
        sendButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bluetoothSPP.send("test123", true);
            }
        });

        Button discoverButton = (Button) findViewById(R.id.discover);
        discoverButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                bluetoothSPP = new BluetoothSPP(MainActivity.this);
                bluetoothSPP.setBluetoothStateListener(new BluetoothSPP.BluetoothStateListener() {
                    @Override
                    public void onServiceStateChanged(int state) {
                        Log.e(TAG, "state: " + state);
                    }
                });
                bluetoothSPP.setBluetoothConnectionListener(new BluetoothSPP.BluetoothConnectionListener() {
                    @Override
                    public void onDeviceConnected(String name, String address) {
                        Log.e(TAG, String.format("connected: name: %s\t address: %s", name, address));
                    }

                    @Override
                    public void onDeviceDisconnected() {
                        Log.e(TAG, "disconnected");
                    }

                    @Override
                    public void onDeviceConnectionFailed() {
                        Log.e(TAG, "connect failed");
                    }
                });
                bluetoothSPP.setupService();
                bluetoothSPP.startService(true);
                String[] names = bluetoothSPP.getPairedDeviceName();
                String[] addresses = bluetoothSPP.getPairedDeviceAddress();
                for (int i = 0; i < names.length; i++) {
                    Log.e(TAG, String.format("name: %s\taddress: %s", names[i], addresses[i]));
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        printer.onActivityResult(requestCode, resultCode, data);
    }
}
